/*
 * Created on May 23, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package schoolsource.model;

import java.util.Enumeration;
import java.util.Hashtable;


/**
 * @author Steven Steele
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class DBStore extends Thread {

    // Map of containers
    public final Hashtable bucket = new Hashtable();

    // The length of time before a timeout occurs
    private static long WAITTIME = 1000 * 60 * 1; // 1 minutes

//  private static long TIMEOUT = 10000 * 60 * 10; // 10 minutes
    private static long TIMEOUT = 10000 * 10; // 10 minutes

    public void run() {
        Enumeration e = null;
        Object currentKey = null;
        BucketTimeWrapper currentElement = null;
        long currentTime = 0;
        while (true) {
            try {
                //  SchoolLogger.logger.log(Level.INFO, "going to sleep");
                sleep(WAITTIME);
                currentTime = System.currentTimeMillis();

                // Check announcementContainerBucket
                e = bucket.keys();
                while (e.hasMoreElements()) {
                    currentKey = e.nextElement();
                    currentElement = (BucketTimeWrapper) bucket.get(currentKey);
                    if (currentTime - currentElement.getLastAccessedTime() > TIMEOUT) {
//SchoolLogger.logger.log(Level.INFO, " Deleting " + currentKey + " from bucket");
                        bucket.remove(currentKey);
                    }
                }

            } catch (InterruptedException ie) {
                // do nothing
            }
        }
    }
    public Container getContainer(String key) {
        BucketTimeWrapper btw = (BucketTimeWrapper) bucket.get(key);
        if (btw == null) {
            return null;
        }
        else {
//SchoolLogger.logger.log(Level.INFO, " ++++++++++++++++++++++ got a cache hit");
            return btw.getValue();
        }
    }

    public void addContainer(Container c, String key) {
        bucket.put(key, new BucketTimeWrapper(c));
    }

    public void removeContainer(String key) {
        bucket.remove(key);
    }

}
